package com.ruiysoft.crm.mapper;

import cn.taroco.common.vo.UserVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.model.vo.UserGroupByDeptVo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author liuht
 * @since 2017-10-29
 */
public interface SysUserMapper extends BaseMapper<SysUser> {
    /**
     * 根据id获取用户信息
     *
     * @param id id
     * @return {@link SysUser}
     */
    @Select("SELECT * FROM sys_user WHERE dept_id IN (SELECT dept_id \n" +
            "FROM (SELECT  t1.dept_id, IF(FIND_IN_SET(parent_id, @pids) > 0,@pids := CONCAT(@pids, ',', dept_id), 0) AS ischild \n" +
            "  FROM (SELECT dept_id,parent_id FROM sys_dept org WHERE org.`del_flag` = 0 \n" +
            "    ORDER BY parent_id, dept_id) t1,(SELECT @pids := #{id }) t2) t3 WHERE ischild != '0' UNION ALL SELECT #{id })")
    List<SysUser> getUsersByParentId(int id);

    /**
     * 根据部门分组的用户数据
     *
     * @return List
     */
    @Select("select  d.name as deptName,u.user_id as userId,u.real_name as realName,d.dept_id  as deptId from sys_dept d,sys_user u where u.dept_id=d.dept_id order by  d.dept_id asc,d.name asc")
    List<UserGroupByDeptVo> getUsersByDept();

    /**
     * 根据用户名获取用户信息
     *
     * @param name 用户名
     * @return {@link UserVO}
     */
    @Select("select * from sys_user u where u.username=#{name }")
    UserVO getUsersByName(String name);

    @Select("select user_Id userId from sys_user user  where user.username=#{userName}")
    int getUserIdByUserName(String userName);
}
